#include "../inc/sopc.h"
#include "../inc/lcd.h"
#include "../inc/delay.h"
#include "../inc/pic.h"
#include "../inc/chinese.h"
#include "../inc/touch.h"
#include "altera_avalon_pio_regs.h"
#include <unistd.h>

int model[8] = {-1};
char display[8];
int pos = 0;

void Load_KeyBoard(void)
{
    LCD_Clear(WHITE);
    POINT_COLOR = BLUE;
    LCD_ShowString(0, 12, "chip model: ");

    // 数字部分横线
    LCD_DrawLine(0, 40, 240, 40);
    LCD_DrawLine(60, 110, 240, 110);
    LCD_DrawLine(60, 180, 240, 180);
    LCD_DrawLine(60, 250, 240, 250);
    // 字母部分横线
    LCD_DrawLine(0, 71, 60, 71);
    LCD_DrawLine(0, 102, 60, 102);
    LCD_DrawLine(0, 133, 60, 133);
    LCD_DrawLine(0, 164, 60, 164);
    LCD_DrawLine(0, 195, 60, 195);
    LCD_DrawLine(0, 226, 60, 226);
    LCD_DrawLine(0, 257, 60, 257);
    LCD_DrawLine(0, 288, 60, 288);
    // 竖线
    LCD_DrawLine(60, 40, 60, 320);
    LCD_DrawLine(120, 40, 120, 320);
    LCD_DrawLine(180, 40, 180, 320);
    // 字母（A B C F H L S T V）
    LCD_ShowChar(26, 47, 65, 16, 0);
    LCD_ShowChar(26, 78, 66, 16, 0);
    LCD_ShowChar(26, 109, 67, 16, 0);
    LCD_ShowChar(26, 140, 70, 16, 0);
    LCD_ShowChar(26, 171, 72, 16, 0);
    LCD_ShowChar(26, 202, 76, 16, 0);
    LCD_ShowChar(26, 233, 83, 16, 0);
    LCD_ShowChar(26, 264, 84, 16, 0);
    LCD_ShowChar(26, 295, 86, 16, 0);
    // 第一行数字
    LCD_ShowChar(86, 67, 49, 16, 0);
    LCD_ShowChar(146, 67, 50, 16, 0);
    LCD_ShowChar(206, 67, 51, 16, 0);
    // 第二行数字
    LCD_ShowChar(86, 137, 52, 16, 0);
    LCD_ShowChar(146, 137, 53, 16, 0);
    LCD_ShowChar(206, 137, 54, 16, 0);
    // 第三行数字
    LCD_ShowChar(86, 207, 55, 16, 0);
    LCD_ShowChar(146, 207, 56, 16, 0);
    LCD_ShowChar(206, 207, 57, 16, 0);
    // 第四行数字
    LCD_ShowChar(86, 277, 60, 16, 0);
    LCD_ShowChar(146, 277, 48, 16, 0);
    LCD_ShowChar(206, 277, 35, 16, 0);
    POINT_COLOR = RED;
}

void display_model()
{
    int i;
    for (i = 0; i < 8; ++i)
    {
        if (model[i] == -1)
        {
            display[7 - i] = ' ';
        }
        else
        {
            display[7 - i] = model[i];
        }
    }
}

int *decimal2binary(int d)
{
    static int b[7] = {0};
    int i = 6;
    while (d != 0)
    {
        b[i] = d % 2;
        d = d / 2;
        i--;
    }
    return b;
}

void send_out()
{
    int message[8][7] = {{0}};
    int i;
    for (i = 0; i < 8; ++i)
    {
        int *temp = decimal2binary(model[i]);
        int j;
        for (j = 0; j < 7; ++j)
        {
            message[i][j] = *(temp + j);
            SEND->DATA = message[i][j];
        }
        delay_ms(50);
    }
}

int main(void)
{

    LCD_Init();

    tp_dev.init();

    Load_KeyBoard();
    while (1)
    {
        tp_dev.scan(0);
        if (tp_dev.sta & TP_PRES_DOWN)
        {
            switch (tp_dev.x / 60)
            {
            case 0:
                switch ((tp_dev.y - 40) / 31)
                {
                case 0:
                    if (pos <= 7)
                    {
                        model[pos++] = 65;
                    }
                    break;
                case 1:
                    if (pos <= 7)
                    {
                        model[pos++] = 66;
                    }
                    break;
                case 2:
                    if (pos <= 7)
                    {
                        model[pos++] = 67;
                    }
                    break;
                case 3:
                    if (pos <= 7)
                    {
                        model[pos++] = 70;
                    }
                    break;
                case 4:
                    if (pos <= 7)
                    {
                        model[pos++] = 72;
                    }
                    break;
                case 5:
                    if (pos <= 7)
                    {
                        model[pos++] = 76;
                    }
                    break;
                case 6:
                    if (pos <= 7)
                    {
                        model[pos++] = 83;
                    }
                    break;
                case 7:
                    if (pos <= 7)
                    {
                        model[pos++] = 84;
                    }
                    break;
                case 8:
                    if (pos <= 7)
                    {
                        model[pos++] = 86;
                    }
                    break;
                default:
                    break;
                }
                break;
            case 1:
                switch ((tp_dev.y - 40) / 70)
                {
                case 0:
                    if (pos <= 7)
                    {
                        model[pos++] = 49;
                    }
                    break;
                case 1:
                    if (pos <= 7)
                    {
                        model[pos++] = 52;
                    }
                    break;
                case 2:
                    if (pos <= 7)
                    {
                        model[pos++] = 55;
                    }
                    break;
                case 3:
                    //退格
                    if (pos == 8)
                        pos--;
                    model[pos] = -1;
                    if (pos > 0)
                        pos--;
                    break;
                default:
                    break;
                }
                break;
            case 2:
                switch ((tp_dev.y - 40) / 70)
                {
                case 0:
                    if (pos <= 7)
                    {
                        model[pos++] = 50;
                    }
                    break;
                case 1:
                    if (pos <= 7)
                    {
                        model[pos++] = 53;
                    }
                    break;
                case 2:
                    if (pos <= 7)
                    {
                        model[pos++] = 56;
                    }
                    break;
                case 3:
                    if (pos <= 7)
                    {
                        model[pos++] = 48;
                    }
                    break;
                default:
                    break;
                }
                break;
            case 3:
                switch ((tp_dev.y - 40) / 70)
                {
                case 0:
                    if (pos <= 7)
                    {
                        model[pos++] = 51;
                    }
                    break;
                case 1:
                    if (pos <= 7)
                    {
                        model[pos++] = 54;
                    }
                    break;
                case 2:
                    if (pos <= 7)
                    {
                        model[pos++] = 57;
                    }
                    break;
                case 3:
                    //确定
                    send_out();
                    break;
                default:
                    break;
                }
                break;
            default:
                break;
            }
            display_model();
            LCD_ShowString(96, 12, display);
        }
        else
            delay_ms(500);
    }
    return 0;
}
